package com.cw.jeeyt.controller.system;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.cw.jeeyt.common.Constants;
import com.cw.jeeyt.common.enums.EnumYn;
import com.cw.jeeyt.common.web.BaseController;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.service.bo.system.SystemNoticeBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.service.directive.FooterDirective;
import com.cw.jeeyt.service.impl.cache.ThemeCache;
import com.cw.jeeyt.service.intf.security.SecurityResourceService;
import com.cw.jeeyt.service.intf.system.SystemNoticeService;

@Controller
@RequestMapping("/system")
public class SystemController extends BaseController {
    @Resource
    private SystemNoticeService systemNoticeService;
    @Resource
    private SecurityResourceService securityResourceService;
    @Resource
    private ThemeCache themeCache;

    /**
     * 跳转到系统后台管理首页
     *
     * @return
     */
    @RequestMapping("/main")
    public ModelAndView toMain() throws Exception {
        ModelAndView mv = new ModelAndView("system/index");
        //加载公告通知页面
        List<SystemNoticeBo> systemNoticeList = systemNoticeService.querySystemNoticeList(EnumYn.Y.getCode());
        StringBuilder sbTitle = new StringBuilder("");
        if (systemNoticeList != null && !systemNoticeList.isEmpty()) {
            for (SystemNoticeBo systemNotice : systemNoticeList) {
                sbTitle.append("<a href='view_notice?id=" + systemNotice.getId() + "&operation=view' target='_blank'>"
                    + systemNotice.getNoticeTitle() + "</a><br/>");
            }
        }
        mv.addObject("noticeContent", sbTitle.toString());
        return mv;
    }

    /**
     * 跳转到系统后台管理首页
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/index")
    public ModelAndView toSystemIndex(HttpServletRequest request) throws Exception {

        SecurityUserBo user = SessionUtil.getSessionUser();
        if (user == null) {
            return null;
        }
        String theme = themeCache.getTheme(user.getId());
        if (StringUtils.isEmpty(theme)) {
            theme = Constants.SYSTEM_THEME_DARK;
        }
        ModelAndView mv = new ModelAndView("system/index");
        //加载公告通知页面
        List<SystemNoticeBo> systemNoticeList = systemNoticeService.querySystemNoticeList(EnumYn.Y.getCode());
        StringBuilder sbTitle = new StringBuilder("");
        if (systemNoticeList != null && !systemNoticeList.isEmpty()) {
            for (SystemNoticeBo systemNotice : systemNoticeList) {
                sbTitle.append("<a href='view_notice?id=" + systemNotice.getId() + "&operation=view' target='_blank'>"
                    + systemNotice.getNoticeTitle() + "</a><br/>");
            }
        }
        //初始化菜单
        String menuInfo = initMenu(request);
        mv.addObject("noticeContent", sbTitle.toString());
        mv.addObject("menuInfo", menuInfo);
        mv.addObject("theme", theme);
        mv.addObject("footer", new FooterDirective());
        mv.addObject("userInfo", user);
        request.setAttribute("userName", user.getAccount());
        return mv;
    }

    private String initMenu(HttpServletRequest request) throws Exception {
        String menuInfo = "";
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        try {
            Long userId = userVo.getId();
            menuInfo = securityResourceService.queryResourceByLoginUser(userId, request);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
        return menuInfo;
    }

    /**
     * 跳转到系统后台管理空白页面
     *
     * @return
     */
    @RequestMapping("/home_blank")
    public ModelAndView toHome() {
        ModelAndView mv = new ModelAndView("system/system_blank");
        return mv;
    }

    /**
     * 跳转到系统后台管理首页
     *
     * @return
     */
    @RequestMapping("/view_notice")
    public ModelAndView viewNotice(String id, String operation) throws Exception {

        ModelAndView mv = new ModelAndView("system/systemNotice_publicForm");
        SystemNoticeBo systemNotice = systemNoticeService.getSystemNoticeById(id);
        mv.addObject("systemNotice", systemNotice);
        mv.addObject("operation", operation);
        return mv;
    }
}
